/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.authentication;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class TNAuthentication {

    public TNAuthentication() {
    }
    
    public Connection cn = null;

    public Connection getCn() {
        return cn;
    }

    public void setCn(Connection cn) {
        this.cn = cn;
    }

    public boolean isAdmin(int userId) throws Exception{
        PreparedStatement pst = null;
        ResultSet rs = null;
        
        String sql = " select a.user_id,b.group_id,c.group_name "
                    + " from sec_user a,sec_group_user b,sec_group c"
                    + " where a.user_id = b.user_id and c.group_id = b.group_id and a.user_id = ? and upper(c.group_name) = upper(?)";
        try{
            pst = cn.prepareStatement(sql);
            
            pst.setInt(1, userId);
            pst.setString(2, getAdminGroupName());
            
            rs = pst.executeQuery();
            
            return rs.next();
        }
        finally{
            Database.closeObject(rs);
            Database.closeObject(pst);
        }
    }
    
    public String getAdminGroupName(){
        ResourceBundle resource = ResourceBundle.getBundle("config");
        String name = resource.getString("adminGroupName");        
        return name;
    }    
}
